import 'package:flutter/material.dart';

import 'package:hotfil_pub_web/controllers/minepage_controller.dart';

import 'package:hotfil_pub_web/pages/mine/widgets/options.dart';
import 'package:hotfil_pub_web/pages/mine/widgets/publish_qr.dart';

import 'package:hotfil_pub_web/shared/choose_image.dart';
import 'package:hotfil_pub_web/shared/gen_publish_post.dart';
import 'package:hotfil_pub_web/shared/http/http.dart';
import 'package:hotfil_pub_web/shared/loading.dart';
import 'package:hotfil_pub_web/shared/request_permission.dart';

import 'package:hotfil_pub_web/widgets/gradient_appbar.dart';
import 'package:hotfil_pub_web/widgets/my_theme.dart';
import 'package:velocity_x/velocity_x.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:get/get.dart';

class MinePage extends StatelessWidget {
  final GlobalKey _refresherKey = GlobalKey();
  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  final BoxDecoration cardBoxDecoration = BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(4),
      boxShadow: [
        BoxShadow(
            color: MyTheme.boxShadowColor,
            offset: const Offset(0, 0),
            blurRadius: 10,
            spreadRadius: 4),
      ]);

  MinePageController minePageController = Get.put(MinePageController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SmartRefresher(
      key: _refresherKey,
      controller: _refreshController,
      onRefresh: () {
        minePageController.fetchMinePageData();
        _refreshController.refreshCompleted();
      },
      child: SingleChildScrollView(
        child: Container(
            color: Colors.white,
            child: Stack(
              children: [
                Container(
                  height: 157,
                  decoration: BoxDecoration(
                      gradient: LinearGradient(colors: [
                    MyTheme.gradientStart,
                    MyTheme.gradientEnd,
                  ])),
                ),
                Column(
                  children: [
                    GetBuilder<MinePageController>(
                      init: minePageController,
                      builder: (_) => GradientAppbar(
                          child: Row(
                        children: [
                          Container(
                            width: 34,
                            height: 34,
                            clipBehavior: Clip.hardEdge,
                            decoration: BoxDecoration(
                              color: MyTheme.lightPrimaryColor,
                              borderRadius:
                                  const BorderRadius.all(Radius.circular(16)),
                            ),
                            child: _.userInfo.avatar.length > 0
                                ? Image.network(_.userInfo.avatar)
                                : null,
                            // const Center(
                            //   child: Icon(Icons.dashboard),
                            // ),
                          ),
                          const SizedBox(
                            width: 10,
                          ),
                          _.userInfo.name!.text.white.size(16).make(),
                          const SizedBox(
                            width: 10,
                          ),
                          _.userInfo.phone!.text.white.size(12).make(),
                          ElevatedButton(
                              onPressed: () async {
                                await requestPhonePermission();
                                await chooseImage(context,
                                    onImageDoneListener: (dynamic data) async {
                                  if (data != null) {
                                    // String base64Image = base64Encode(data);
                                    // var a = Image.memory(data);
                                    // print(a);
                                    startLoading(loaingTxt: '头像上传中');
                                    // await Https().post('/users/avatar/set',
                                    //     data: FormData.fromMap({
                                    //       "file": await MultipartFile.fromBytes(
                                    //           data,
                                    //           filename:
                                    //               _.userInfo.name! + '.png')
                                    //     }));
                                    _.fetchMinePageData();
                                  }
                                });
                              },
                              child: Text('上传头像'))
                        ],
                      )),
                    ),
                    Container(
                      width: MediaQuery.of(context).size.width - 40,
                      padding: EdgeInsets.all(20),
                      margin: const EdgeInsets.only(left: 20, right: 20),
                      decoration: cardBoxDecoration,
                      child: Column(
                        children: [
                          Text('FIL资产'),
                          Text('FIL资产'),
                          Text('FIL资产'),
                          Text('FIL资产'),
                          Text('FIL资产'),
                          Text('FIL资产'),
                          ElevatedButton(
                              onPressed: () {
                                genPublishPost(context);
                              },
                              child: const Text('生成海报')),
                          PublishQr()
                        ],
                      ),
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    Container(
                      width: MediaQuery.of(context).size.width - 40,
                      padding: EdgeInsets.all(20),
                      margin:
                          const EdgeInsets.only(left: 20, right: 20, top: 10),
                      decoration: cardBoxDecoration,
                      child: Column(
                        children: [
                          Text('FIL资产'),
                          Text('FIL资产'),
                          Text('FIL资产'),
                          Text('FIL资产'),
                          Text('FIL资产'),
                          Text('FIL资产'),
                        ],
                      ),
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    Container(
                      color: Colors.white,
                      child: Column(
                        children: [
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                          MineOptions(),
                        ],
                      ),
                    )
                  ],
                )
              ],
            )),
      ),
    ));
  }
}
